<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            border-collapse: collapse;
            text-align: center;
            margin: 50px auto;
        }
    </style>
</head>

<body>
    <table border="1" width="400">
        <thead>
            <th>
                <input type="checkbox" name="" id="">全选
            </th>
            <th>
                名字
            </th>
            <th>
                价钱
            </th>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input type="checkbox" name="" id="">
                </td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" name="" id="">
                </td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
    <script src="./jquery.js"></script>
    <script>
        var thc = document.querySelector('thead th input');
        var tb = document.querySelector('tbody');
        var cartGoods = [
            {
                goodsname: '小米10',
                price: 5000,
                isChecked: true,//代表是否选中了
            },
            {
                goodsname: '苹果10',
                price: 3000,
                isChecked: false,//代表是否选中了
            }
        ];
        var tri = '';
        for (var i = 0; i < cartGoods.length; i++) {
            tri += '<tr>\
                        <td>\
                            <input type="checkbox" name="" id="">\
                        </td>\
                        <td>' + cartGoods[i].goodsname + '</td>\
                        <td>' + cartGoods[i].price + '</td>\
                    </tr>';
            tb.innerHTML = tri;
        };
        $('thead :checkbox').click(function(){
            $('tbody :checkbox').prop('checked',$(this).prop('checked'))
        })
        $('tbody :checkbox').on('click',function(){
            if ($('tbody :checkbox:checked').length == $('tbody :checkbox').length) {
                console.log("全选了");
                $('thead :checkbox').prop('checked',true);
            } else {
                console.log("未全选");
                $('thead :checkbox').prop('checked',false);
            }
        }) 
    </script>
</body>

</html>